/*
 * 文件名： JiuFragment.java
 * 描    述：MainActivity -- 九块邮
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.miguo.activity.base.BaseFragment;
import com.miguo.activity.callback.IFragment;
import com.miguo.app.AppConfig;
import com.miguo.framework.view.pagerindicator.UnderlinePageIndicator;
import com.miguo.plugins.Statistics;
import com.miguo.ui.R;

import java.util.ArrayList;

public class JiuFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener, IFragment {

    private View contentView;

    private RadioGroup mJiuRg;
    private ViewPager mPagerView;
    private UnderlinePageIndicator mLineIindicator;

    private ArrayList<Fragment> fragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_jiu, container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViewById();
        initView();
    }

//    private boolean firstLoad = true;
//    @Override
//    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
//
//        Animation anim = AnimationUtils.loadAnimation(getActivity(), nextAnim);
//
//        anim.setAnimationListener(new AnimationListener() {
//
//            @Override
//            public void onAnimationStart(Animation animation) {
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                if (firstLoad) {
//                    firstLoad = false;
//                    initView();
//                }
//            }
//        });
//
//        return anim;
//    }

    private void findViewById() {
        mJiuRg = (RadioGroup) contentView.findViewById(R.id.jiu_rg);
        mLineIindicator = (UnderlinePageIndicator) contentView.findViewById(R.id.line_indicator);
        mPagerView = (ViewPager) contentView.findViewById(R.id.pager_view);
    }

    private void initView() {
        fragment = new ArrayList<Fragment>();
        JiuListFragment jiu_rb1 = JiuListFragment.newInstance(String.valueOf(1));
        fragment.add(jiu_rb1);
        JiuListFragment jiu_rb2 = JiuListFragment.newInstance(String.valueOf(2));
        fragment.add(jiu_rb2);

        FragmentPagerAdapter mPagerAdapter = new PagerAdapter(getChildFragmentManager());
        mPagerView.setAdapter(mPagerAdapter);
        mLineIindicator.setOnPageChangeListener(this);
        mLineIindicator.setViewPager(mPagerView);
        mJiuRg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.jiu_rb1:
                mPagerView.setCurrentItem(0);
                break;
            case R.id.jiu_rb2:
                mPagerView.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            mJiuRg.check(R.id.jiu_rb1);
        } else {
            mJiuRg.check(R.id.jiu_rb2);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }


    @Override
    public void onStartFragment() {
        Statistics.onTrackCustomClick(mContext, AppConfig.STATISTICS.EVENT_NAME_3);
    }

    @Override
    public void onStopFragment() {
    }

    private class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragment.get(position);
        }

        @Override
        public int getCount() {
            return fragment.size();
        }
    }

}
